# Copyright (c) 2022, hzmes and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document
from frappe.utils import getdate, today

class SRMAlterMold(Document):
	def autoname(self):
		if self.billno:
			self.name = self.billno


	def before_insert(self):
		if not self.billno:
			current_billno = "GMSQ"
			current_billno += "".join(today().split("-"))
			series = frappe.db.get_value('SRM Naming Series',
								{'doctype_name': self.doctype, 'date': today()},
								['name', 'current_series'], as_dict=True)
			if series:
				current_billno += str(series.current_series).zfill(3)
			else:
				frappe.get_doc({'doctype': 'SRM Naming Series', 'doctype_name': self.doctype, 'date': today()}).insert(ignore_permissions=True)
				current_billno += "001"
			self.billno = current_billno
			frappe.db.set_value('SRM Naming Series', series.name, 'current_series', series.current_series + 1)
